﻿using AccountModule;
using Framework;
using UnityEngine;
using UnityEngine.UI;

namespace View
{
    public class LoginView : ViewProxy
    {
        private InputField _inputName;
        private ClickTrigger _triggerLogin;
        private ClickTrigger _triggerRegist;
        
        protected override void SetupView()
        {
            LoadRes("Views/Login/LoginView");
        }

        protected override void OnInit()
        {
            _inputName = _transform.Find("Input_Name").GetComponent<InputField>();
            _triggerLogin = ClickTrigger.Get(_transform.Find("Button_Login"));
            _triggerRegist = ClickTrigger.Get(_transform.Find("Text_Regist"));
            
            _triggerLogin.AddClickListener(OnLoginClick);
            _triggerRegist.AddClickListener(OnRegistClick);
        }

        private void OnLoginClick(params object[] args)
        {
            string name = _inputName.text;
            Account account = AccountManager.instance.GetAccount(name);
            if (account != null)
            {
                AccountManager.instance.accountName = account.name;
                
                new MyRecipeView().Show();
                Close();
            }
        }

        private void OnRegistClick(params object[] args)
        {
            new RegistView().Show();
            Close();
        }
    }
}